package com.study.jmx.demo;

import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.lang.management.ManagementFactory;

//MBean的接口和实现类必须位于同一个包中

public class Hello implements HelloMBean {
    public String name;

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String print(String msg) {
        String result = "Hello," + name + "!!" + msg;
        System.out.println(result);
        return result;
    }

    public static void main(String args[]) throws Exception {
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); // 获取MBeanServer
        ObjectName name = new ObjectName("fjs:type=hello"); // 构造一个名字
        Hello hello = new Hello(); // 创建需要注册的对象
        mbs.registerMBean(hello, name); // 注册这个对象

        //mbs.setAttribute(name, new Attribute("Name", "bianjie"));//属性首字母必须大写
        //Object[] params = {" ^^^"};
        //String[] signature = {String.class.getName()};
        //mbs.invoke(name, "print", params, signature);//第三个参数为Object[],为传入的参数值，第四个参数为String[],指明参数类型
        Thread.sleep(Long.MAX_VALUE);
    }
}
